package com.northpool.operator.common.datasource;

import com.northpool.operator.annotation.InputParam;
import com.northpool.operator.annotation.InputParams;
import com.northpool.operator.annotation.OperatorInfo;
import com.northpool.operator.annotation.OutputParam;
import com.northpool.operator.annotation.OutputParams;
import com.northpool.operator.common.algorithm.AlgorithmBase;
import com.northpool.operator.common.database.DatabaseUtils;
import com.northpool.operator.common.database.PasswordCrypo;
import com.northpool.operator.common.database.bean.NacosProperties;
import com.northpool.operator.type.TypeEnum;
import org.apache.commons.lang3.StringUtils;

import java.util.LinkedHashMap;
import java.util.Map;

@OperatorInfo(
        ref = "DataSourceEncryptInfoOperator",
        version = "1.0.0-3",
        name = "数据源信息获取",
        description = "数据中心加密数据源信息获取（三个数据源）"
)
@OutputParams({
        @OutputParam(ref = "dataSourceInfo1", name = "加密数据源信息1"),
        @OutputParam(ref = "dataSourceInfo2", name = "加密数据源信息2"),
        @OutputParam(ref = "dataSourceInfo3", name = "加密数据源信息3")
})
public class DataSourceEncryptInfoOperator3 extends AlgorithmBase {

    @InputParams({
            @InputParam(ref = "serverAddr", name = "Nacos服务地址", desc = "Nacos服务地址, 示例: 192.168.164.45:8848", required = true, value = ""),
            @InputParam(ref = "namespace", name = "命名空间", desc = "Nacos命名空间，示例: dev", required = true, value = ""),
            @InputParam(ref = "username", name = "用户名", desc = "Nacos用户名", required = true, value = "", type = TypeEnum.STRING),
            @InputParam(ref = "password", name = "密码", desc = "Nacos密码", required = true, value = "", type = TypeEnum.STRING),
            @InputParam(ref = "group", name = "分组", desc = "Nacos配置分组", required = true, value = ""),
            @InputParam(ref = "dataCenterId1", name = "第一个数据中心数据源ID", desc = "数据中心数据源ID", required = true, value = ""),
            @InputParam(ref = "dataCenterId2", name = "第二个数据中心数据源ID", desc = "数据中心数据源ID", required = true, value = ""),
            @InputParam(ref = "dataCenterId3", name = "第三个数据中心数据源ID", desc = "数据中心数据源ID", required = true, value = ""),
    })
    public DataSourceEncryptInfoOperator3() {
    }

    public static void main(String[] args) {
        // 写一个测试用例
        Map<String, String> param = new LinkedHashMap<>();
        param.put("serverAddr", "192.168.164.45:8848");
        param.put("namespace", "dev");
        param.put("username", "nacos");
        param.put("password", "admin");
        param.put("group", "dev");
        param.put("dataCenterId1", "402881ac93c476360193cb4d554a02fd");
        param.put("dataCenterId2", "402881ac93507d500193509b772e0003");
        param.put("dataCenterId3", "402881ac93c476360193cb4d554a02fd");

        DataSourceEncryptInfoOperator3 op = new DataSourceEncryptInfoOperator3();
        op.execute(param);

    }

    @Override
    public void execute(Map<String, String> params) {
        String dataCenterId1 = params.get("dataCenterId1");
        String dataCenterId2 = params.get("dataCenterId2");
        String dataCenterId3 = params.get("dataCenterId3");
        if (StringUtils.isBlank(dataCenterId1) || StringUtils.isBlank(dataCenterId2) || StringUtils.isBlank(dataCenterId3)) {
            throw new IllegalArgumentException("dataSourceId 不能为空");
        }
        NacosProperties properties = new NacosProperties();
        properties.setServerAddr(params.get("serverAddr"));
        properties.setNamespace(params.get("namespace"));
        properties.setUsername(params.get("username"));
        properties.setPassword(params.get("password"));
        properties.setGroup(params.get("group"));

        DatabaseUtils.init(properties);

        String dataSourceJson1 = DatabaseUtils.getDataSourceJson(dataCenterId1);
        logResult("dataSourceInfo1", PasswordCrypo.encode(dataSourceJson1));

        String dataSourceJson2 = DatabaseUtils.getDataSourceJson(dataCenterId2);
        logResult("dataSourceInfo2", PasswordCrypo.encode(dataSourceJson2));

        String dataSourceJson3 = DatabaseUtils.getDataSourceJson(dataCenterId3);
        logResult("dataSourceInfo3", PasswordCrypo.encode(dataSourceJson3));
    }
}
